package com.lzy.mrdark.adapter;


import java.util.ArrayList;

import com.lzy.mrdark.R;
import com.lzy.mrdark.widget.PagerSlidingTabStrip;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
/**
 *初始化标签组和对应的ViewPager
 * 
 * @author dark
 * @version 1.0
 * @created 2016年3月24日
 * 参考文献：http://blog.csdn.net/dreamzml/article/details/9951577
 */
@SuppressLint("Recycle")
public class ViewPageFragmentAdapter extends FragmentStatePagerAdapter {

	 private final Context mContext;
	    protected PagerSlidingTabStrip mPagerStrip;
	    private final ViewPager mViewPager;
	    private final ArrayList<ViewPageInfo> mTabs = new ArrayList<ViewPageInfo>();

	    public ViewPageFragmentAdapter(FragmentManager fm,
	            PagerSlidingTabStrip pageStrip, ViewPager pager) {
	        super(fm);
	        mContext = pager.getContext();
	        mPagerStrip = pageStrip;
	        mViewPager = pager;
	        mViewPager.setAdapter(this);
	        //给标签组设置对应的ViewPager
	        mPagerStrip.setViewPager(mViewPager);
	    }

	    public void addTab(String title, String tag, Class<?> clss, Bundle args) {
	        ViewPageInfo viewPageInfo = new ViewPageInfo(title, tag, clss, args);
	        addFragment(viewPageInfo);
	    }

	    public void addAllTab(ArrayList<ViewPageInfo> mTabs) {
	        for (ViewPageInfo viewPageInfo : mTabs) {
	            addFragment(viewPageInfo);
	        }
	    }

	    private void addFragment(ViewPageInfo info) {
	        if (info == null) {
	            return;
	        }

	        // 加入tab title
	        View v = LayoutInflater.from(mContext).inflate(
	                R.layout.base_viewpage_fragment_tab_item, null, false);
	        TextView title = (TextView) v.findViewById(R.id.tab_title);
	        title.setText(info.title);
	        mPagerStrip.addTab(v);

	        mTabs.add(info);
	        notifyDataSetChanged();
	    }

	    /**
	     * 移除第一次
	     */
	    public void remove() {
	        remove(0);
	    }

	    /**
	     * 移除一个tab
	     * 
	     * @param index
	     *            备注：如果index小于0，则从第一个开始删 如果大于tab的数量值则从最后一个开始删除
	     */
	    public void remove(int index) {
	        if (mTabs.isEmpty()) {
	            return;
	        }
	        if (index < 0) {
	            index = 0;
	        }
	        if (index >= mTabs.size()) {
	            index = mTabs.size() - 1;
	        }
	        mTabs.remove(index);
	        mPagerStrip.removeTab(index, 1);
	        notifyDataSetChanged();
	    }

	    /**
	     * 移除所有的tab
	     */
	    public void removeAll() {
	        if (mTabs.isEmpty()) {
	            return;
	        }
	        mPagerStrip.removeAllTab();
	        mTabs.clear();
	        //使修改立马生效
	        notifyDataSetChanged();
	    }

	    @Override
	    public int getCount() {
	        return mTabs.size();
	    }

	    @Override
	    public int getItemPosition(Object object) {
	        return PagerAdapter.POSITION_NONE;
	    }

	    /**
	     * 获取对应的标签页实体对象，并根据标签页实体属性生成对应的Fragment
	     */
	    @Override
	    public Fragment getItem(int position) {
	        ViewPageInfo info = mTabs.get(position);
	        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
	    }
	    /**
	     * 获取标签页的名称
	     */
	    @Override
	    public CharSequence getPageTitle(int position) {
	        return mTabs.get(position).title;
	    }
}